/*
编写 : Hexh
时间 : 2012-7-2 17:51:39
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Entities;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of InfraIdcard
	/// </summary>
	public class InfraIdcardController : BaseController
	{

		private readonly IInfraIdcardFacade InfraIdcardFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public InfraIdcardController(IInfraIdcardFacade InfraIdcardFacade, IHospUserFacade HospUserFacade)
		{
			this.InfraIdcardFacade = InfraIdcardFacade;
			this.HospUserFacade = HospUserFacade;
		}

		[Description("访问IC卡管理页面")]
		[ViewPage]
		public ActionResult InfraIdcard()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改IC卡信息")]
		public ActionResult SaveOrUpdate()
		{
			InfraIdcard entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				var isFieldExist = InfraIdcardFacade.IsFieldExist("CardUid", Request["CardUid"], "-1");
				if(isFieldExist)
					return this.JsonFormat(new ExtResult { success = !isFieldExist, msg = "已存在相同的项!" });

				entity = new InfraIdcard
				{
					CardUid = Request["CardUid"],
					Type = (InfraIdcardType)Convert.ToInt32(Request["Type"]),
					DeptId = Request["DeptId"],
					State = "0"
				};
			}
			else
			{
				// get the entity.
				entity = InfraIdcardFacade.Get(Request["Id"]);
				if (entity.State != "0")
					throw new ValidationException("IC卡已使用,不可修改。");
				// set the value
				entity.Type = (InfraIdcardType)Convert.ToInt32(Request["Type"]);
				entity.DeptId = Request["DeptId"];
			}
			// 保存信息
			InfraIdcardFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除InfraIdcard基本资料")]
		public ActionResult Delete()
		{
			InfraIdcard entity = InfraIdcardFacade.Get(Request["Id"]);
			if (entity.State != "0")
				throw new ValidationException("IC卡已使用,不可删除。");
			
			InfraIdcardFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除InfraIdcard基本资料")]
		public ActionResult PhysicsDelete()
		{
			InfraIdcard entity = InfraIdcardFacade.Get(Request["Id"]);
			if (entity.State != "0")
				throw new ValidationException("IC卡已使用,不可删除。");

			InfraIdcardFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取InfraIdcard清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = InfraIdcardFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<InfraIdcard> { total = total, data = data },
				new string[] { "Department" });
		}
		
		[Description("获取InfraIdcard信息")]
		public ActionResult GetById()
		{
			var entity = InfraIdcardFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<InfraIdcard> { data = new InfraIdcard[] { entity } }, new string[] { "Department" }
			   );
		}

		[ExtResult]
		[Description("判断InfraIdcard字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = InfraIdcardFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
